﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace Cashier.usercontrols
{
    /// <summary>
    /// 分隔线
    /// </summary>
    public class SeparateLine: Control
    {
        private Color _lineColor;

        [System.ComponentModel.Category("自定义属性"), System.ComponentModel.Description("控件的大小（以像素为单位）")]
        public new Size Size
        {
            get
            {
                return base.Size;
            }
            set
            {
                // 注意这里，这个控件高度已经在代码中被定死，无论怎么修改都是3像素，如果嫌小，可以手工修改下面的代码。
                int height = 3;
                if (direction.Equals("v"))
                    height = value.Height;
                SetBounds(Location.X, Location.Y, value.Width, height, BoundsSpecified.Size);
            }
        }

        public new Color BackColor
        {
            get { return base.BackColor; }
            set
            {
                base.BackColor = Color.Transparent;
            }
        }

        private String direction = "h";

        [Category("Custom Attribute")]
        [Description("分隔线方向：h-横向(默认) v-纵向")]
        [Browsable(true)]
        public String Direction
        {
            get { return direction; }
            set { direction = value; }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            Rectangle r = e.ClipRectangle;
            Point left = new Point(r.Location.X, r.Location.Y + 1);
            Point right = new Point(r.Location.X + r.Width, r.Location.Y + 1);
            if(this.direction.Equals("v"))
                right = new Point(r.Location.X, r.Location.Y + 200 + r.Height);

            // 下面的代码就是 #cccccc 颜色代码，深灰色，有要修改的人可以手工改，或者扩展下，变成属性直接可以定义。
            _lineColor = Color.FromArgb(204, 204, 204);
            Pen pen = new Pen(new SolidBrush(_lineColor), 1.5f);
            g.DrawLine(pen, left, right);
            pen.Dispose();
        }

    }
}
